<template>
  <div class="app-container flex-col minH">
    <div class="topline w100p bgwhite pt20 pb10 radius10 pl10 pr20 justify-between">
      <div>
        <el-input v-model="uid" style="width: 280px" placeholder="搜尋用戶ID..." :prefix-icon="Search" class="search mr10 mb10" />
        <el-select v-model="type" placeholder="選擇項目類型" size="large" style="width: 240px" class="mr10 mb10">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <div class="align-center mb10 mr10">
          <el-date-picker v-model="start" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
          <span class="plr10 clr9fa">--</span>
          <el-date-picker v-model="end" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
        </div>

      </div>
      <div class="mb10">
        <el-button class="btn2" @click="getDiscountList()">查詢</el-button>
        <el-button class="btn1" @click="reset(1)">重置</el-button>
      </div>
    </div>
    <div class="bgblueg ptb30 radius10 w100p mt15 plr80 justify-between">
      <div class="align-center clr3c4 flex1">
        <div>
          <div class="ft46">{{ current_month }}</div>
          <div>本月目前紅利（元）</div>
        </div>
        <div class="ml6p">
          <div class="ft46">{{ current_month_total }}</div>
          <div>本月縂紅利（元）</div>
        </div>
        <!-- <div class="ml6p">
          <div class="ft46">88889</div>
          <div>本月目前紅利（元）</div>
        </div> -->
      </div>
      <el-button class="btn2" @click="dialogShow()">
        <el-icon class="mr5">
          <Histogram />
        </el-icon>

        本月紅利排行榜</el-button>
    </div>
    <div class="mt15 radius10 hidden pb15 flex1 bgwhite  flex-col w100p">
      <div class="flex1 w100p ">
        <el-table :data="tableData" style="width: 100%" cell-class-name="tbcell" header-cell-class-name="hdcell">
          <el-table-column prop="user" label="用戶訊息" width="200">
            <template #default="scope">

              <span @click="showUser(scope.row.user.id)">{{ scope.row.user.phone }}(ID:{{ scope.row.user.id }})</span>
            </template>
          </el-table-column>
          <el-table-column prop="user" label="用戶昵稱" min-width="200">
            <template #default="scope">
              <span>{{ scope.row.user.name }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="money" label="充值金額" min-width="250" sortable />
          <el-table-column prop="discount" label="充值紅利" min-width="250" sortable />
          <el-table-column prop="type_text" label="優惠類型" min-width="250" />
          <el-table-column prop="created_at" label="創建時間" min-width="200" />
        </el-table>

      </div>
      <div class="plr20 mlauto mt20">
        <el-pagination v-model:current-page="currentPage" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>

    </div>
    <el-dialog v-model="dialogTableVisible" title="本月紅利排行榜">
      <div class="plr20 justify-between ptb20">
        <div class="align-center mb10 mr10">
          <el-date-picker v-model="start2" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
          <span class="plr10 clr9fa">--</span>
          <el-date-picker v-model="end2" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
        </div>

        <div class="noshrink">
          <el-button class="btn2" @click="getDisTopList()">查詢</el-button>
          <el-button class="btn1" @click="reset(2)">重置</el-button>
        </div>

      </div>
      <el-table :data="disTableData" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
        <el-table-column type="index" label="ID" width="100" />
        <el-table-column property="uid" label="用戶訊息" min-width="200">
          <template #default="scope">
            <span>{{ scope.row.user.name }} (ID:{{ scope.row.user.id }})</span>
          </template>
        </el-table-column>
        <el-table-column property="discount" label="領取金額" />

      </el-table>
      <div class="  plr20 justify-between mt20">
        <div style="flex:1;" class="clr3c4 bold">總共{{ disTotal }}條</div>
        <!-- <div>小計(元): <span class="ft46 clr3c4 ml10">{{ current_month_total }}</span> </div> -->
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router';
import { discountList, disTopList } from '@/api/statement.js'
import { inject } from 'vue';
const showUser = inject('showUser');
onMounted(() => {
  getDiscountList()
})

// 优惠报表
const getDiscountList = () => {

  discountList({
    start: start.value,
    end: end.value,
    type: type.value,
    uid: uid.value,
    page: currentPage.value
  }).then((res) => {
    console.log('优惠报表', res)
    tableData.value = res.list.data
    total.value = res.list.total
    current_month.value = res.current_month
    current_month_total.value = res.current_month_total
  })

}
const reset = (resetType) => {
  if (resetType == 1) {
    start.value = ''
    end.value = ''
    type.value = ''
    uid.value = ''
    getDiscountList()
  } else {
    start2.value = ''
    end2.value = ''
    getDisTopList()
  }

}

const start2 = ref('')
const end2 = ref('')
// 红利排行榜
const getDisTopList = () => {
  disTopList({
    start: start2.value,
    end: end2.value
  }).then((res) => {
    console.log('获取红利', res)
    disTotal.value = res.list.total
    disTableData.value = res.list.data
  })
}
const dialogTableVisible = ref(false)

const current_month = ref('')
const current_month_total = ref('')
const type = ref('')
const uid = ref('')
const start = ref('')
const end = ref('')
const options = [
  {
    value: '1',
    label: '充值',
  },
  {
    value: '2',
    label: 'vip',
  },
]
const tableData = ref([])
const disTotal = ref(0)//红利总数据
const disTableData = ref([])//红利表格
const tableRowClassName = () => {
  return 'tbrow'
}
const currentPage = ref(1)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  getDiscountList()
}

const dialogShow = () => {
  dialogTableVisible.value = true
  getDisTopList()
}
const router = useRouter()
const toAdd = () => {
  console.log(router)
  router.push({ path: 'addNotice' })
}
const edit = (type) => {
  router.push({ path: 'signEdit' })
}
</script>

<style lang="scss" scoped>
.search {
  overflow: hidden;
  border-radius: 10px !important;
  // padding: 5px 0px;
}
</style>
<style></style>
